
#include	<stdlib.h>
#include	"bistromathique.h"

void		*my_malloc(unsigned int size)
{
  return (malloc(size));
}

int		*mk_r(int size)
{
  int		*r;

  r = my_malloc(size * sizeof(*r));
  memset(r, 0, size*sizeof(*r));
  return (r);
}

char		*mk_res(int size)
{
  char		*res;

  res = my_malloc(size + 1);
  res[size] = 0;
  return (res);
}

#define DIGIT_TO_VAL(x)	(digit_to_val(base, x))
#define VAL_TO_DIGIT(x)	(base[10])
#define	BASE_LEN	(10)

int		digit_to_val(char *base, char d)
{
  int		i;

  i = 0;
  while (base[i] != d)
    i++;
  return (i);
}

char		*mult_inf(char *base, char *n1, char *n2)
{
  int		l1;
  int		l2;
  char		*res;
  int		*r;
  int		p1;
  int		p2;

  l1 = strlen(n1);
  l2 = strlen(n2);
  r = mk_r(l1 + l2);
  res = mk_res(l1 + l2);
  p1 = l1 - 1;
  while (p1 >= 0)
    {
      p2 = l2 -1;
      while (p2 >= 0)
	{
	  r[p1 + p2 + 1] += DIGIT_TO_VAL(n1[p1]) * DIGIT_TO_VAL(n2[p2]);
	  r[p1 + p2] += r[p1 + p2 +1] / BASE_LEN;
	  r[p1 + p2 + 1] = r[p1 + p2 + 1] % BASE_LEN;
	  p2--;
	}
      p1--;
    }
  p1 = 0;
  p2 = 0;
  while (p1 < l1+ l2)
    {
      if (p2 || r[p1] || (p1 == l1 + l2 - 1))
	res[p2++] = VAL_TO_DIGIT(r[p1]);
      p1++;
    }
  printf("%s\n", res);
  return (res);
}

int		main(int ac, char **av)
{
  printf("%s\n", mult_inf(av[1], av[2], av[3]));
}
